home *** CD-ROM | disk | FTP | other *** search
/ Ian & Stuart's Australian Mac 1993 September / September 93.iso / Archives / Utilities / Security - care / Protect / Tonto / FakeAlert.c next >
Text File  |  1991-11-26  |  5KB  |  194 lines

  1. //# include    <Dialogs.h>
  2.  
  3. #ifndef    nil
  4. # define    nil        (0L)
  5. # define    false    0
  6. # define    true    1
  7. #endif
  8.  
  9.  
  10. /*
  11.     In-memory item list for dialog with four items:
  12.  
  13.     1    "^0^1^2^3" (static text)
  14.     2    Button 1
  15.     3    Button 2
  16.     4    Button 3
  17.  
  18.     The caller of FakeAlert passes the four strings that are to be
  19.     substituted into the first item, the number of buttons that
  20.     should be used, and the titles to put into each button.
  21.     A copy of the item list is hacked to use the right number of
  22.     buttons.
  23.  
  24.     Thanks to Erik Kilk and Jason Haines.  Some of the stuff to do
  25.     this is modified from code they wrote.
  26. */
  27.  
  28.  
  29. static int    itemList [] =
  30. {
  31.     3,                    /* max number of items - 1 */
  32.  
  33. /*
  34.     statText item
  35. */
  36.     0, 0,                /* reserve a long for item handle */
  37.     10, 27, 61, 225,    /* display rectangle */
  38.     ((8+128) << 8) | 8,    /* 8 + 128 = statText (disabled), title 8 bytes long */
  39.     '^0', '^1',        /* ^0^1^2^3 */
  40.     '^2', '^3',
  41.  
  42. /*
  43.     first button
  44. */
  45.  
  46.     0, 0,                /* reserve a long for item handle */
  47.     104, 140, 124, 210,    /* display rectangle */
  48.     (4 << 8) | 0,        /* 4 = pushButton, title is 0 bytes long*/
  49.  
  50. /*
  51.     second button
  52. */
  53.  
  54.     0, 0,                /* reserve a long for item handle */
  55.     104, 30, 124, 100,    /* display rectangle */
  56.     (4 << 8) | 0,        /* 4 = pushButton, title is 0 bytes long */
  57.  
  58. /*
  59.     third button
  60. */
  61.  
  62.     0, 0,                /* reserve a long for item handle */
  63.     72, 30, 92, 100,    /* display rectangle */
  64.     (4 << 8) | 0        /* 4 = pushButton, title is 0 bytes long */
  65. };
  66.  
  67. static SetDControl (DialogPtr, int, StringPtr, int);
  68. void BugAlert (StringPtr);
  69. int FakeAlert (StringPtr, StringPtr, StringPtr, StringPtr, int, int,
  70.                         StringPtr, StringPtr, StringPtr);
  71. void OSError (StringPtr, OSErr, StringPtr);
  72.  
  73. /*
  74.     Set dialog button title and draw bold outline if makeBold true.
  75.     This must be done after the window is shown or else the bold
  76.     outline won't show up (which is probably the wrong way to do it).
  77. */
  78.  
  79. SetDControl (theDialog, itemNo, title, makeBold)
  80. DialogPtr    theDialog;
  81. int            itemNo;
  82. StringPtr    title;
  83. int        makeBold;
  84. {
  85. Handle        itemHandle;
  86. short        itemType;
  87. Rect        itemRect;
  88. PenState    pState;
  89.  
  90.     GetDItem (theDialog, itemNo, &itemType, &itemHandle, &itemRect);
  91.     SetCTitle ((ControlHandle)itemHandle, title);
  92.     if (makeBold)
  93.     {
  94.         GetPenState (&pState);
  95.         PenNormal ();
  96.         PenSize (3, 3);
  97.         InsetRect (&itemRect, -4, -4);
  98.         FrameRoundRect (&itemRect, 16, 16);
  99.         SetPenState (&pState);
  100.     }
  101. }
  102.  
  103.  
  104. /*
  105.     Fake an alert, using an in-memory window and item list.
  106.     The message to be presented is constructed from the first
  107.     four arguments.  nButtons is the number of buttons to use,
  108.     defButton is the default button, the next three args are
  109.     the titles to put into the buttons.  The return value is
  110.     the button number (1..nButtons).  This must be interpreted
  111.     by the caller, since the buttons may be given arbitrary
  112.     titles.
  113.  
  114.     nButtons should be between 1 and 3, inclusive.
  115.     defButton should be between 1 and nButtons, inclusive.
  116. */
  117.  
  118.  
  119. FakeAlert (s1, s2, s3, s4, nButtons, defButton, t1, t2, t3)
  120. StringPtr    s1, s2, s3, s4;
  121. int            nButtons;
  122. int            defButton;
  123. StringPtr    t1, t2, t3;
  124. {
  125. GrafPtr        savePort;
  126. register DialogPtr    theDialog;
  127. register Handle        iListHandle;
  128. Rect        bounds;
  129. short        itemHit;
  130.  
  131.     InitCursor ();
  132.     GetPort (&savePort);
  133.     iListHandle = NewHandle (512L);
  134.     HLock (iListHandle);
  135.     BlockMove (&itemList, *iListHandle, 512L);
  136.     ((int *) *iListHandle)[0] = nButtons;    /* = number items - 1 */
  137.     SetRect (&bounds, 115, 80, 355, 220);
  138.     theDialog = NewDialog (nil, &bounds, "\p", false, dBoxProc, (WindowPtr)-1L,
  139.                             false, 0L, iListHandle);
  140.  
  141.     ParamText (s1, s2, s3, s4);        /* construct message */
  142.  
  143.     SetPort (theDialog);
  144.     ShowWindow (theDialog);
  145.  
  146.     switch (nButtons)                /* set button titles */
  147.     {
  148.         case 3:
  149.             SetDControl (theDialog, 4, t3, defButton == 3);
  150.             /* fall through... */
  151.         case 2:
  152.             SetDControl (theDialog, 3, t2, defButton == 2);
  153.             /* fall through... */
  154.         case 1:
  155.             SetDControl (theDialog, 2, t1, defButton == 1);
  156.     }
  157.  
  158. /*
  159.     ModalDialog returns 1 if return/enter hit, which, since
  160.     the statText item is first, can be unambiguously
  161.     interpreted as "choose default".
  162. */
  163.     ModalDialog (nil, &itemHit);
  164.     itemHit = (itemHit == 1 ? defButton : itemHit - 1);
  165.     HUnlock (iListHandle);
  166.     /*HPurge (iListHandle);*/
  167.     DisposDialog (theDialog);
  168.     SetPort (savePort);
  169.     return (itemHit);
  170. }
  171.  
  172. void
  173. BugAlert(s)
  174. StringPtr    s;
  175. {
  176.     FakeAlert (s, (StringPtr)"\p", (StringPtr)"\p", (StringPtr)"\p", 1, 1,
  177.         (StringPtr)"\pok", (StringPtr)"\p", (StringPtr)"\p");
  178. }
  179.  
  180.  
  181. void
  182. OSError(s, n, t)
  183. StringPtr    s, t;
  184. OSErr        n;
  185. {
  186.     Str255    numstr;
  187.     long    errnum;
  188.  
  189.     errnum = n;
  190.     NumToString (errnum, numstr);
  191.     FakeAlert (s, t, (StringPtr)"\p error = ", numstr, 1, 1,
  192.         (StringPtr)"\pSigh", (StringPtr)"\p", (StringPtr)"\p");
  193.     ExitToShell ();
  194. }